package com.ttmusic.view.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ttmusic.activitys.R;
import com.ttmusic.comparator.ArtistInfoComparator;
import com.ttmusic.comparator.MusicInfoComparator;
import com.ttmusic.core.MusicManager;
import com.ttmusic.entity.AbstractMusic;
import com.ttmusic.entity.ArtistInfo;
import com.ttmusic.entity.MusicInfo;
import com.ttmusic.utils.ChineseToEnglish;
import com.ttmusic.utils.Logs;
import com.ttmusic.view.listeners.OnItemClickListener;


/**
 * ListView展示艺术家列表时必须的适配器
 * @author Administrator
 *
 */
public class ArtistAdapter extends BaseAdapter implements SectionIndexer {


    private Context mContext;
    private List<ArtistInfo> list = new ArrayList<ArtistInfo>();
    //private OnItemClickListener listener;
    private boolean isABC;
    
    public ArtistAdapter(Context mContext, List<ArtistInfo> list, boolean isABC) {
        this.mContext = mContext;
        this.list = list;
        this.isABC = isABC;
        //对歌曲进行排序
        if(isABC)    Collections.sort(this.list, new ArtistInfoComparator());
//        for(ArtistInfo a : list) {
//        	String s = a.getPinYinHeadCharofArtist().charAt(0) + "-->" + a.getArtistId() + " " + a.getArtist() + " " + a.getAlbumArt();
//        	Logs.writeLog("测试歌手排序：", s);
//        }
    }
    
    /**
     * 改变数据了-用在扫描本地歌曲完成之后
     * @param newMusicList
     */
    public void changeData(List<ArtistInfo> newArtistInfoList) {
    	list = newArtistInfoList;
    	//对歌曲进行排序
        if(isABC)    Collections.sort(this.list, new ArtistInfoComparator());
        this.notifyDataSetChanged();
    }
    
    @SuppressWarnings("rawtypes")
	public List getList() {
        return list;
    }

//    public void setOnItemClickListener(OnItemClickListener listener) {
//        this.listener = listener;
//    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * 
     * @param position 当前行号
     * @param convertView 当前行
     * @param parent 父级元素
     */
    @SuppressLint("InflateParams")
	@Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder mViewHolder = null;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_artist, null);
            mViewHolder.icon_artist = (ImageView) convertView.findViewById(R.id.icon_artist_album);
            mViewHolder.letter_text = (TextView) convertView.findViewById(R.id.artist_catalog);
            mViewHolder.artist_name = (TextView) convertView.findViewById(R.id.artist_name);
            mViewHolder.artist_songnums = (TextView) convertView.findViewById(R.id.artist_songnums);
            mViewHolder.icon_more_operation = (ImageView)convertView.findViewById(R.id.icon_more_operation);
            mViewHolder.artist_item = (LinearLayout) convertView.findViewById(R.id.artist_item);
            mViewHolder.icon_more_operation.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Toast.makeText(mContext, "更多被点击了", Toast.LENGTH_SHORT).show();
				}
			});
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        //Song song = (Song) getItem(position);
        ArtistInfo artistInfo = (ArtistInfo) getItem(position);

        //String title = isABC ? "" : (position+1)+".";
        mViewHolder.artist_name.setText(artistInfo.getArtist());
        mViewHolder.artist_songnums.setText(artistInfo.getNumSongs()+"首");
        mViewHolder.icon_artist.setImageBitmap(artistInfo.getArtistBitMap(mContext.getResources()));
        
        if(isABC) {
            int section = getSectionForPosition(position);
            if (position == getPositionForSection(section)) {
                mViewHolder.letter_text.setVisibility(View.VISIBLE);
                String letterText = artistInfo.getPinYinHeadCharofArtist();
                mViewHolder.letter_text.setText(letterText.charAt(0)+"");
            } else {
                mViewHolder.letter_text.setVisibility(View.GONE);
            }
        }else{
            mViewHolder.letter_text.setVisibility(View.GONE);
        }
//        mViewHolder.artist_item.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if(listener!=null){
//                    listener.onItemClick(position);
//                    notifyDataSetChanged();
//                }
//            }
//        });
        return convertView;
    }

    


    final static class ViewHolder {
    	ImageView icon_artist;
    	TextView letter_text;
        TextView artist_name;
        TextView artist_songnums;
        ImageView icon_more_operation;
        LinearLayout artist_item;
    }

    @Override
    public Object[] getSections() {
        return null;
    }

    @Override
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            ArtistInfo art = (ArtistInfo) getItem(i);
            String sortStr = art.getPinYinHeadCharofArtist();
            char firstchar = sortStr.charAt(0);
            if (firstchar == section) return i;
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
    	ArtistInfo art = (ArtistInfo) getItem(position);
    	String str = art.getPinYinHeadCharofArtist();
    	return str.charAt(0);
    }

}
